package com.lhf.userservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lhf.common.enums.Status;
import com.lhf.common.util.Result;
import com.lhf.userservice.entity.FvsSysUser;
import com.lhf.userservice.service.FvsSysUserService;
import com.lhf.userservice.vo.FvsSysAddRoleForUserVo;
import com.lhf.userserviceapi.dto.FvsSysUserDTO;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.Optional;

/**
 * (FvsSysUser)表控制层
 *
 * @author liuhf
 * @since 2021-03-30 08:38:40
 */
@RestController
@RequestMapping("/sysUser")
public class FvsSysUserController {
    /**
     * 服务对象
     */
    @Resource
    private FvsSysUserService fvsSysUserService;
    @Resource(name = "passwordEncoder")
    private PasswordEncoder passwordEncoder;

    /**
     * 分页查询所有数据
     *
     * @param page       分页对象
     * @param fvsSysUser 查询实体
     * @return 所有数据
     */
    @GetMapping("findAndLimit")
    public Result<IPage<FvsSysUser>> selectAll(Page<FvsSysUser> page, FvsSysUser fvsSysUser) {
        page.setHitCount(true);
        return Result.success(this.fvsSysUserService.page(page, new QueryWrapper<>(fvsSysUser)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne/{id}")
    public Result<FvsSysUser> selectOne(@PathVariable Serializable id) {
        return Result.success(this.fvsSysUserService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param fvsSysUser 实体对象
     * @return 新增结果
     */
    @PostMapping("save")
    public Result<Boolean> insert(@RequestBody FvsSysUser fvsSysUser) {
        fvsSysUser.setPassword(passwordEncoder.encode(fvsSysUser.getPassword()));
        return Result.success(this.fvsSysUserService.save(fvsSysUser));
    }

    /**
     * 修改数据
     *
     * @param fvsSysUser 实体对象
     * @return 修改结果
     */
    @PutMapping("modify")
    public Result<Boolean> update(@RequestBody FvsSysUser fvsSysUser) {
        Optional.ofNullable(fvsSysUser.getPassword()).ifPresent(item -> fvsSysUser.setPassword(this.passwordEncoder.encode(fvsSysUser.getPassword())));
        return Result.success(this.fvsSysUserService.updateById(fvsSysUser));
    }

    /**
     * 删除数据
     *
     * @param uid 用户id
     * @return 删除结果
     */
    @DeleteMapping("del/{id}")
    public Result<Boolean> delete(@PathVariable("id") Serializable uid) {
        return Result.success(this.fvsSysUserService.removeById(uid));
    }

    /**
     * 给用户添加角色
     *
     * @param vo
     * @return
     */
    @PostMapping("addRoles")
    public Result<Boolean> addRoleByUid(@RequestBody FvsSysAddRoleForUserVo vo) {
        return Result.success(this.fvsSysUserService.addRoleByUid(vo.getUid(), vo.getRids()));
    }


    /**
     * 删除角色
     *
     * @param vo
     * @return
     */
    @DeleteMapping("delRoles")
    public Result<Boolean> delRoleByUid(@RequestBody FvsSysAddRoleForUserVo vo) {
        return Result.success(this.fvsSysUserService.delRoleByUid(vo));
    }


//    /**
//     * 根据用户名查询用户
//     *
//     * @param username
//     * @return
//     */
//    @GetMapping("/findUserByUsername/{username}")
//    public Result<FvsSysUserDTO> findUserByUsername(@PathVariable("username") String username) {
//        return Result.success(this.fvsSysUserService.findUserByUsername(username));
//    }


    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public Result<Object> sqlIntegrityConstraintViolationException(SQLIntegrityConstraintViolationException e) {
        e.printStackTrace();
        return Result.failure(Status.ALREADY_EXIST);
    }
}
